using Microsoft.IdentityModel.Protocols.WSTrust.Bindings;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Net35.Xrm.Sdk.Client
{
	internal static class ServiceMetadataUtility
	{
		public static IssuerEndpointDictionary RetrieveIssuerEndpoints(System.ServiceModel.EndpointAddress issuerMetadataAddress)
		{
			IssuerEndpointDictionary issuerEndpointDictionary = new IssuerEndpointDictionary();
			System.ServiceModel.Description.MetadataExchangeClient metadataExchangeClient = ServiceMetadataUtility.CreateMetadataClient(issuerMetadataAddress.Uri.Scheme);
			System.ServiceModel.Description.MetadataSet metadata = metadataExchangeClient.GetMetadata(issuerMetadataAddress.Uri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
			if (metadata != null)
			{
				System.ServiceModel.Description.WsdlImporter wsdlImporter = new System.ServiceModel.Description.WsdlImporter(metadata);
				System.ServiceModel.Description.ServiceEndpointCollection serviceEndpointCollection = wsdlImporter.ImportAllEndpoints();
				foreach (System.ServiceModel.Description.ServiceEndpoint current in serviceEndpointCollection)
				{
					if (!(current.Binding is System.ServiceModel.NetTcpBinding))
					{
						TokenServiceCredentialType tokenServiceCredentialType = TokenServiceCredentialType.None;
						System.ServiceModel.Security.TrustVersion trustVersion = System.ServiceModel.Security.TrustVersion.Default;
						System.ServiceModel.WS2007HttpBinding wS2007HttpBinding = current.Binding as System.ServiceModel.WS2007HttpBinding;
						if (wS2007HttpBinding != null)
						{
							System.ServiceModel.Channels.BindingElementCollection bindingElementCollection = wS2007HttpBinding.CreateBindingElements();
							System.ServiceModel.Channels.SecurityBindingElement securityBindingElement = bindingElementCollection.Find<System.ServiceModel.Channels.SecurityBindingElement>();
							if (securityBindingElement != null)
							{
								trustVersion = securityBindingElement.MessageSecurityVersion.TrustVersion;
								if (trustVersion == System.ServiceModel.Security.TrustVersion.WSTrust13)
								{
									if (wS2007HttpBinding.Security.Message.ClientCredentialType == System.ServiceModel.MessageCredentialType.UserName)
									{
										tokenServiceCredentialType = TokenServiceCredentialType.Username;
									}
									else if (wS2007HttpBinding.Security.Message.ClientCredentialType == System.ServiceModel.MessageCredentialType.Certificate)
									{
										tokenServiceCredentialType = TokenServiceCredentialType.Certificate;
									}
									else if (wS2007HttpBinding.Security.Message.ClientCredentialType == System.ServiceModel.MessageCredentialType.Windows)
									{
										tokenServiceCredentialType = TokenServiceCredentialType.Windows;
									}
								}
							}
						}
						else
						{
							System.ServiceModel.Channels.BindingElementCollection bindingElementCollection2 = current.Binding.CreateBindingElements();
							System.ServiceModel.Channels.SecurityBindingElement securityBindingElement2 = bindingElementCollection2.Find<System.ServiceModel.Channels.SecurityBindingElement>();
							if (securityBindingElement2 != null)
							{
								trustVersion = securityBindingElement2.MessageSecurityVersion.TrustVersion;
								if (trustVersion == System.ServiceModel.Security.TrustVersion.WSTrust13)
								{
									System.ServiceModel.Security.Tokens.IssuedSecurityTokenParameters issuedTokenParameters = ServiceMetadataUtility.GetIssuedTokenParameters(securityBindingElement2);
									if (issuedTokenParameters != null)
									{
										if (issuedTokenParameters.KeyType == System.IdentityModel.Tokens.SecurityKeyType.SymmetricKey)
										{
											tokenServiceCredentialType = TokenServiceCredentialType.SymmetricToken;
										}
										else if (issuedTokenParameters.KeyType == System.IdentityModel.Tokens.SecurityKeyType.AsymmetricKey)
										{
											tokenServiceCredentialType = TokenServiceCredentialType.AsymmetricToken;
										}
										else if (issuedTokenParameters.KeyType == System.IdentityModel.Tokens.SecurityKeyType.BearerKey)
										{
											tokenServiceCredentialType = TokenServiceCredentialType.Bearer;
										}
									}
									else
									{
										System.ServiceModel.Security.Tokens.KerberosSecurityTokenParameters kerberosTokenParameters = ServiceMetadataUtility.GetKerberosTokenParameters(securityBindingElement2);
										if (kerberosTokenParameters != null)
										{
											tokenServiceCredentialType = TokenServiceCredentialType.Kerberos;
										}
									}
								}
							}
						}
						if (tokenServiceCredentialType != TokenServiceCredentialType.None)
						{
							string key = tokenServiceCredentialType.ToString();
							if (!issuerEndpointDictionary.ContainsKey(key))
							{
								issuerEndpointDictionary.Add(key, new IssuerEndpoint
								{
									IssuerAddress = current.Address,
									IssuerBinding = current.Binding,
									IssuerMetadataAddress = issuerMetadataAddress,
									CredentialType = tokenServiceCredentialType,
									TrustVersion = trustVersion
								});
							}
						}
					}
				}
			}
			return issuerEndpointDictionary;
		}
		public static IssuerEndpointDictionary RetrieveACSIssuerEndpoints(System.Uri trustUrl)
		{
			return new IssuerEndpointDictionary
			{

				{
					TokenServiceCredentialType.SymmetricToken.ToString(),
					new IssuerEndpoint
					{
						CredentialType = TokenServiceCredentialType.SymmetricToken,
						IssuerAddress = new System.ServiceModel.EndpointAddress(new System.Uri(trustUrl.AbsoluteUri + "v2/wstrust/13/issuedtoken-symmetric"), new System.ServiceModel.Channels.AddressHeader[0]),
						TrustVersion = System.ServiceModel.Security.TrustVersion.WSTrust13,
						IssuerBinding = new IssuedTokenWSTrustBinding
						{
							TrustVersion = System.ServiceModel.Security.TrustVersion.WSTrust13,
							SecurityMode = System.ServiceModel.SecurityMode.TransportWithMessageCredential,
							KeyType = System.IdentityModel.Tokens.SecurityKeyType.SymmetricKey
						}
					}
				},

				{
					TokenServiceCredentialType.Bearer.ToString(),
					new IssuerEndpoint
					{
						CredentialType = TokenServiceCredentialType.Bearer,
						IssuerAddress = new System.ServiceModel.EndpointAddress(new System.Uri(trustUrl.AbsoluteUri + "v2/wstrust/13/issuedtoken-bearer"), new System.ServiceModel.Channels.AddressHeader[0]),
						TrustVersion = System.ServiceModel.Security.TrustVersion.WSTrust13,
						IssuerBinding = new IssuedTokenWSTrustBinding
						{
							TrustVersion = System.ServiceModel.Security.TrustVersion.WSTrust13,
							SecurityMode = System.ServiceModel.SecurityMode.TransportWithMessageCredential,
							KeyType = System.IdentityModel.Tokens.SecurityKeyType.BearerKey
						}
					}
				},

				{
					TokenServiceCredentialType.AsymmetricToken.ToString(),
					new IssuerEndpoint
					{
						CredentialType = TokenServiceCredentialType.AsymmetricToken,
						IssuerAddress = new System.ServiceModel.EndpointAddress(new System.Uri(trustUrl.AbsoluteUri + "v2/wstrust/13/issuedtoken-asymmetric"), new System.ServiceModel.Channels.AddressHeader[0]),
						TrustVersion = System.ServiceModel.Security.TrustVersion.WSTrust13,
						IssuerBinding = new IssuedTokenWSTrustBinding
						{
							TrustVersion = System.ServiceModel.Security.TrustVersion.WSTrust13,
							SecurityMode = System.ServiceModel.SecurityMode.TransportWithMessageCredential,
							KeyType = System.IdentityModel.Tokens.SecurityKeyType.AsymmetricKey
						}
					}
				}
			};
		}
		public static IssuerEndpointDictionary RetrieveLiveIdIssuerEndpoints(IdentityProviderTrustConfiguration identityProviderTrustConfiguration)
		{
			IssuerEndpointDictionary issuerEndpointDictionary = new IssuerEndpointDictionary();
			issuerEndpointDictionary.Add(TokenServiceCredentialType.Username.ToString(), new IssuerEndpoint
			{
				CredentialType = TokenServiceCredentialType.Username,
				IssuerAddress = new System.ServiceModel.EndpointAddress(identityProviderTrustConfiguration.Endpoint.AbsoluteUri),
				IssuerBinding = identityProviderTrustConfiguration.Binding
			});
			IssuedTokenWSTrustBinding issuedTokenWSTrustBinding = new IssuedTokenWSTrustBinding
			{
				TrustVersion = identityProviderTrustConfiguration.TrustVersion,
				SecurityMode = identityProviderTrustConfiguration.SecurityMode
			};
			issuedTokenWSTrustBinding.KeyType = System.IdentityModel.Tokens.SecurityKeyType.BearerKey;
			issuerEndpointDictionary.Add(TokenServiceCredentialType.SymmetricToken.ToString(), new IssuerEndpoint
			{
				CredentialType = TokenServiceCredentialType.SymmetricToken,
				IssuerAddress = new System.ServiceModel.EndpointAddress(identityProviderTrustConfiguration.Endpoint.AbsoluteUri),
				IssuerBinding = issuedTokenWSTrustBinding
			});
			return issuerEndpointDictionary;
		}
		public static IssuerEndpointDictionary RetrieveDefaultIssuerEndpoint(AuthenticationProviderType authenticationProviderType, IssuerEndpoint issuer)
		{
			IssuerEndpointDictionary issuerEndpointDictionary = new IssuerEndpointDictionary();
			if (issuer != null && issuer.IssuerAddress != null)
			{
				TokenServiceCredentialType tokenServiceCredentialType;
				switch (authenticationProviderType)
				{
				case AuthenticationProviderType.Federation:
					tokenServiceCredentialType = TokenServiceCredentialType.Kerberos;
					break;
				case AuthenticationProviderType.LiveId:
					tokenServiceCredentialType = TokenServiceCredentialType.Username;
					break;
				case AuthenticationProviderType.OnlineFederation:
					tokenServiceCredentialType = TokenServiceCredentialType.Username;
					break;
				default:
					tokenServiceCredentialType = TokenServiceCredentialType.Kerberos;
					break;
				}
				issuerEndpointDictionary.Add(tokenServiceCredentialType.ToString(), new IssuerEndpoint
				{
					CredentialType = tokenServiceCredentialType,
					IssuerAddress = issuer.IssuerAddress,
					IssuerBinding = issuer.IssuerBinding
				});
			}
			return issuerEndpointDictionary;
		}
		public static IssuerEndpointDictionary RetrieveIssuerEndpoints(AuthenticationProviderType authenticationProviderType, ServiceEndpointDictionary endpoints, bool queryMetadata)
		{
			foreach (System.ServiceModel.Description.ServiceEndpoint current in endpoints.Values)
			{
				try
				{
					IssuerEndpoint issuer = ServiceMetadataUtility.GetIssuer(current.Binding);
					if (issuer != null)
					{
						IssuerEndpointDictionary result;
						if (queryMetadata && issuer.IssuerMetadataAddress != null)
						{
							result = ServiceMetadataUtility.RetrieveIssuerEndpoints(issuer.IssuerMetadataAddress);
							return result;
						}
						result = ServiceMetadataUtility.RetrieveDefaultIssuerEndpoint(authenticationProviderType, issuer);
						return result;
					}
				}
				catch (Exception)
				{
				}
			}
			return new IssuerEndpointDictionary();
		}
		public static IssuerEndpoint GetIssuer(System.ServiceModel.Channels.Binding binding)
		{
			if (binding == null)
			{
				return null;
			}
			System.ServiceModel.Channels.BindingElementCollection bindingElementCollection = binding.CreateBindingElements();
			System.ServiceModel.Channels.SecurityBindingElement securityElement = bindingElementCollection.Find<System.ServiceModel.Channels.SecurityBindingElement>();
			System.ServiceModel.Security.Tokens.IssuedSecurityTokenParameters issuedTokenParameters = ServiceMetadataUtility.GetIssuedTokenParameters(securityElement);
			if (issuedTokenParameters != null)
			{
				return new IssuerEndpoint
				{
					IssuerAddress = issuedTokenParameters.IssuerAddress,
					IssuerBinding = issuedTokenParameters.IssuerBinding,
					IssuerMetadataAddress = issuedTokenParameters.IssuerMetadataAddress
				};
			}
			return null;
		}
		private static System.ServiceModel.Security.Tokens.KerberosSecurityTokenParameters GetKerberosTokenParameters(System.ServiceModel.Channels.SecurityBindingElement securityElement)
		{
			if (securityElement != null && securityElement.EndpointSupportingTokenParameters != null && securityElement.EndpointSupportingTokenParameters.Endorsing != null && securityElement.EndpointSupportingTokenParameters.Endorsing.Count > 0)
			{
				return securityElement.EndpointSupportingTokenParameters.Endorsing[0] as System.ServiceModel.Security.Tokens.KerberosSecurityTokenParameters;
			}
			return null;
		}
		private static System.ServiceModel.Security.Tokens.IssuedSecurityTokenParameters GetIssuedTokenParameters(System.ServiceModel.Channels.SecurityBindingElement securityElement)
		{
			if (securityElement != null && securityElement.EndpointSupportingTokenParameters != null)
			{
				if (securityElement.EndpointSupportingTokenParameters.Endorsing != null && securityElement.EndpointSupportingTokenParameters.Endorsing.Count > 0)
				{
					System.ServiceModel.Security.Tokens.IssuedSecurityTokenParameters issuedSecurityTokenParameters = securityElement.EndpointSupportingTokenParameters.Endorsing[0] as System.ServiceModel.Security.Tokens.IssuedSecurityTokenParameters;
					if (issuedSecurityTokenParameters != null)
					{
						return issuedSecurityTokenParameters;
					}
					System.ServiceModel.Security.Tokens.SecureConversationSecurityTokenParameters secureConversationSecurityTokenParameters = securityElement.EndpointSupportingTokenParameters.Endorsing[0] as System.ServiceModel.Security.Tokens.SecureConversationSecurityTokenParameters;
					if (secureConversationSecurityTokenParameters != null)
					{
						if (secureConversationSecurityTokenParameters.BootstrapSecurityBindingElement.EndpointSupportingTokenParameters.Endorsing.Count > 0)
						{
							return secureConversationSecurityTokenParameters.BootstrapSecurityBindingElement.EndpointSupportingTokenParameters.Endorsing[0] as System.ServiceModel.Security.Tokens.IssuedSecurityTokenParameters;
						}
						if (secureConversationSecurityTokenParameters.BootstrapSecurityBindingElement.EndpointSupportingTokenParameters.Signed.Count > 0)
						{
							return secureConversationSecurityTokenParameters.BootstrapSecurityBindingElement.EndpointSupportingTokenParameters.Signed[0] as System.ServiceModel.Security.Tokens.IssuedSecurityTokenParameters;
						}
					}
				}
				else if (securityElement.EndpointSupportingTokenParameters.Signed != null && securityElement.EndpointSupportingTokenParameters.Signed.Count > 0)
				{
					return securityElement.EndpointSupportingTokenParameters.Signed[0] as System.ServiceModel.Security.Tokens.IssuedSecurityTokenParameters;
				}
			}
			return null;
		}
		public static System.ServiceModel.Channels.CustomBinding SetIssuer(System.ServiceModel.Channels.Binding binding, IssuerEndpoint issuerEndpoint)
		{
			System.ServiceModel.Channels.BindingElementCollection bindingElementCollection = binding.CreateBindingElements();
			System.ServiceModel.Channels.SecurityBindingElement securityElement = bindingElementCollection.Find<System.ServiceModel.Channels.SecurityBindingElement>();
			System.ServiceModel.Security.Tokens.IssuedSecurityTokenParameters issuedTokenParameters = ServiceMetadataUtility.GetIssuedTokenParameters(securityElement);
			if (issuedTokenParameters != null)
			{
				issuedTokenParameters.IssuerAddress = issuerEndpoint.IssuerAddress;
				issuedTokenParameters.IssuerBinding = issuerEndpoint.IssuerBinding;
				if (issuerEndpoint.IssuerMetadataAddress != null)
				{
					issuedTokenParameters.IssuerMetadataAddress = issuerEndpoint.IssuerMetadataAddress;
				}
			}
			return new System.ServiceModel.Channels.CustomBinding(bindingElementCollection);
		}
		private static void ParseEndpoints(ServiceEndpointDictionary serviceEndpoints, System.ServiceModel.Description.ServiceEndpointCollection serviceEndpointCollection)
		{
			serviceEndpoints.Clear();
			if (serviceEndpointCollection != null)
			{
				foreach (System.ServiceModel.Description.ServiceEndpoint current in serviceEndpointCollection)
				{
					if (ServiceMetadataUtility.IsEndpointSupported(current))
					{
						serviceEndpoints.Add(current.Name, current);
					}
				}
			}
		}
		private static bool IsEndpointSupported(System.ServiceModel.Description.ServiceEndpoint endpoint)
		{
			return endpoint != null && !endpoint.Address.Uri.AbsolutePath.EndsWith("web", StringComparison.OrdinalIgnoreCase);
		}
		internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata(Type contractType, System.Uri serviceUri, bool checkForSecondary)
		{
			ServiceEndpointMetadata serviceEndpointMetadata = new ServiceEndpointMetadata();
			serviceEndpointMetadata.ServiceUrls = ServiceConfiguration<IOrganizationService>.CalculateEndpoints(serviceUri);
			System.Uri uri = new System.Uri(serviceUri.AbsoluteUri + "?wsdl");
			System.ServiceModel.Description.MetadataExchangeClient metadataExchangeClient = ServiceMetadataUtility.CreateMetadataClient(uri.Scheme);
			if (metadataExchangeClient != null)
			{
				try
				{
					serviceEndpointMetadata.ServiceMetadata = metadataExchangeClient.GetMetadata(uri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
				}
				catch (InvalidOperationException ex2)
				{
					bool flag = true;
					if (checkForSecondary)
					{
						System.Net.WebException ex = ex2.InnerException as System.Net.WebException;
						if (ex != null && (ex.Status == System.Net.WebExceptionStatus.NameResolutionFailure || ex.Status == System.Net.WebExceptionStatus.Timeout) && serviceEndpointMetadata.ServiceUrls != null)
						{
							if (serviceEndpointMetadata.ServiceUrls.PrimaryEndpoint == serviceUri)
							{
								flag = ServiceMetadataUtility.TryRetrieveMetadata(metadataExchangeClient, new System.Uri(serviceEndpointMetadata.ServiceUrls.AlternateEndpoint.AbsoluteUri + "?wsdl"), serviceEndpointMetadata);
							}
							else if (serviceEndpointMetadata.ServiceUrls.AlternateEndpoint == serviceUri)
							{
								flag = ServiceMetadataUtility.TryRetrieveMetadata(metadataExchangeClient, new System.Uri(serviceEndpointMetadata.ServiceUrls.PrimaryEndpoint.AbsoluteUri + "?wsdl"), serviceEndpointMetadata);
							}
						}
					}
					if (flag)
					{
						throw;
					}
				}
			}
			ClientExceptionHelper.ThrowIfNull(serviceEndpointMetadata.ServiceMetadata, "STS Metadata");
			Collection<System.ServiceModel.Description.ContractDescription> collection = ServiceMetadataUtility.CreateContractCollection(contractType);
			if (collection != null)
			{
				System.ServiceModel.Description.WsdlImporter wsdlImporter = new System.ServiceModel.Description.WsdlImporter(serviceEndpointMetadata.ServiceMetadata);
				System.Collections.Generic.KeyedByTypeCollection<System.ServiceModel.Description.IWsdlImportExtension> wsdlImportExtensions = wsdlImporter.WsdlImportExtensions;
				List<System.ServiceModel.Description.IPolicyImportExtension> policyImportExtensions = ServiceMetadataUtility.AddSecurityBindingToPolicyImporter(wsdlImporter);
				System.ServiceModel.Description.WsdlImporter wsdlImporter2 = new System.ServiceModel.Description.WsdlImporter(serviceEndpointMetadata.ServiceMetadata, policyImportExtensions, wsdlImportExtensions);
				foreach (System.ServiceModel.Description.ContractDescription current in collection)
				{
					wsdlImporter2.KnownContracts.Add(ServiceMetadataUtility.GetPortTypeQName(current), current);
				}
				System.ServiceModel.Description.ServiceEndpointCollection serviceEndpointCollection = wsdlImporter2.ImportAllEndpoints();
				if (wsdlImporter2.Errors.Count > 0)
				{
					foreach (System.ServiceModel.Description.MetadataConversionError current2 in wsdlImporter2.Errors)
					{
						serviceEndpointMetadata.MetadataConversionErrors.Add(current2);
					}
				}
				ServiceMetadataUtility.ParseEndpoints(serviceEndpointMetadata.ServiceEndpoints, serviceEndpointCollection);
			}
			return serviceEndpointMetadata;
		}
		private static List<System.ServiceModel.Description.IPolicyImportExtension> AddSecurityBindingToPolicyImporter(System.ServiceModel.Description.WsdlImporter importer)
		{
			List<System.ServiceModel.Description.IPolicyImportExtension> list = new List<System.ServiceModel.Description.IPolicyImportExtension>();
			System.Collections.Generic.KeyedByTypeCollection<System.ServiceModel.Description.IPolicyImportExtension> policyImportExtensions = importer.PolicyImportExtensions;
			System.ServiceModel.Channels.SecurityBindingElementImporter securityBindingElementImporter = policyImportExtensions.Find<System.ServiceModel.Channels.SecurityBindingElementImporter>();
			if (securityBindingElementImporter != null)
			{
				policyImportExtensions.Remove<System.ServiceModel.Channels.SecurityBindingElementImporter>();
			}
			else
			{
				securityBindingElementImporter = new System.ServiceModel.Channels.SecurityBindingElementImporter();
			}
			list.Add(new AuthenticationPolicyImporter(securityBindingElementImporter));
			list.AddRange(policyImportExtensions);
			return list;
		}
		private static bool TryRetrieveMetadata(System.ServiceModel.Description.MetadataExchangeClient mcli, System.Uri serviceEndpoint, ServiceEndpointMetadata serviceEndpointMetadata)
		{
			bool result = true;
			try
			{
				serviceEndpointMetadata.ServiceMetadata = mcli.GetMetadata(serviceEndpoint, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
				serviceEndpointMetadata.ServiceUrls.GeneratedFromAlternate = true;
				result = false;
			}
			catch
			{
			}
			return result;
		}
		private static System.Xml.XmlQualifiedName GetPortTypeQName(System.ServiceModel.Description.ContractDescription contract)
		{
			return new System.Xml.XmlQualifiedName(contract.Name, contract.Namespace);
		}
		private static Collection<System.ServiceModel.Description.ContractDescription> CreateContractCollection(Type contract)
		{
			return new Collection<System.ServiceModel.Description.ContractDescription>
			{
				System.ServiceModel.Description.ContractDescription.GetContract(contract)
			};
		}
		private static System.ServiceModel.Description.MetadataExchangeClient CreateMetadataClient(string scheme)
		{
			System.ServiceModel.WSHttpBinding wSHttpBinding;
			if (string.Compare(scheme, "https", StringComparison.OrdinalIgnoreCase) == 0)
			{
				wSHttpBinding = (System.ServiceModel.Description.MetadataExchangeBindings.CreateMexHttpsBinding() as System.ServiceModel.WSHttpBinding);
			}
			else
			{
				wSHttpBinding = (System.ServiceModel.Description.MetadataExchangeBindings.CreateMexHttpBinding() as System.ServiceModel.WSHttpBinding);
			}
			wSHttpBinding.MaxReceivedMessageSize = 2147483647L;
			return new System.ServiceModel.Description.MetadataExchangeClient(wSHttpBinding)
			{
				ResolveMetadataReferences = true,
				MaximumResolvedReferences = 100
			};
		}
		public static void ReplaceEndpointAddress(System.ServiceModel.Description.ServiceEndpoint endpoint, System.Uri adddress)
		{
			endpoint.Address = new System.ServiceModel.EndpointAddressBuilder(endpoint.Address)
			{
				Uri = adddress
			}.ToEndpointAddress();
		}
		internal static void AdjustUserNameForWindows(System.ServiceModel.Description.ClientCredentials clientCredentials)
		{
			ClientExceptionHelper.ThrowIfNull(clientCredentials, "clientCredentials");
			if (StringExtensions.IsNullOrWhiteSpace(clientCredentials.UserName.UserName))
			{
				return;
			}
			System.Net.NetworkCredential clientCredential;
			if (clientCredentials.UserName.UserName.IndexOf('@') > -1)
			{
				string[] array = clientCredentials.UserName.UserName.Split(new char[]
				{
					'@'
				});
				if (array.Length > 1)
				{
					clientCredential = new System.Net.NetworkCredential(array[0], clientCredentials.UserName.Password, array[1]);
				}
				else
				{
					clientCredential = new System.Net.NetworkCredential(array[0], clientCredentials.UserName.Password);
				}
			}
			else if (clientCredentials.UserName.UserName.IndexOf('\\') > -1)
			{
				string[] array2 = clientCredentials.UserName.UserName.Split(new char[]
				{
					'\\'
				});
				if (array2.Length > 1)
				{
					clientCredential = new System.Net.NetworkCredential(array2[1], clientCredentials.UserName.Password, array2[0]);
				}
				else
				{
					clientCredential = new System.Net.NetworkCredential(array2[0], clientCredentials.UserName.Password);
				}
			}
			else
			{
				clientCredential = new System.Net.NetworkCredential(clientCredentials.UserName.UserName, clientCredentials.UserName.Password);
			}
			clientCredentials.Windows.ClientCredential = clientCredential;
			clientCredentials.UserName.UserName = string.Empty;
			clientCredentials.UserName.Password = string.Empty;
		}
	}
}
